<?php

declare(strict_types=1);

namespace App\Controller;

use App\Constants\ErrorCode;
use App\Library\Common;
use App\Model\data\CurrentShopData;
use App\Service\FlyGourmetMealService;
use Hyperf\DbConnection\Db;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Validation\Rule;

class GourmetMealController extends AbstractController
{
    //    订餐设置
    /**
     * @Inject
     * @var FlyGourmetMealService
     * */
    private $gourmetMeal;

//    美食管理列表
    public function index()
    {
        $param =  $this->request->inputs(['key_word','is_illegal','check_status','status','is_show','push_by','order_by']);
        $shop_info = CurrentShopData::getData();
        $return = $this->paginate($this->gourmetMeal->index(intval($shop_info->id),$param,2))->toArray();
        $return['count_stat'] =$this->gourmetMeal->countSize(intval($shop_info->id),2);
        return $this->jsonResponse->success($return,'店铺美食列表');
    }

    //    查看店铺美食列表
    public function public_index()
    {
        $param =  $this->request->inputs(['key_word','is_illegal','check_status','shop_id','status','is_show','push_by','order_by']);
        $this->validation($param,
            [
                'shop_id'=>[
                    "required",
                    "integer",
                    "min:1",
                ],
            ]
        );
        $return = $this->paginate($this->gourmetMeal->index(intval($param['shop_id']),$param,1))->toArray();
        $return['count_stat'] =$this->gourmetMeal->countSize(intval($param['shop_id']),2);
        return $this->jsonResponse->success($return,'店铺美食列表');
    }

//   美食详情
    public function detail()
    {
        $id =  $this->request->input('id','');
        $lat = $this->request->input('lat','');
        $lng = $this->request->input('lng','');
        $this->validation([
                'id'=>$id,
                'lng'=>$lng,
                'lat'=>$lat
            ],
            [
                'id'=>[
                    "required",
                    "integer",
                    "min:1",
                    Rule::exists('fly_product')->whereNull('deleted_at'),
                ],
                'lng'=> 'numeric|min:0|max:180',
                'lat'=> 'numeric|min:-90|max:90',
            ]
        );
        return $this->jsonResponse->success($this->gourmetMeal->info(intval($id),$lng,$lat),'店铺美食详情');
    }
//     美食添加
    public function add()
    {
        $shop_info = CurrentShopData::getData();
        $request_p =  $this->request->inputs(['product_name','category_id','fileinfo_id','main_pic','sku']);
        $this->validation($request_p,
            [
                'category_id'=>[
                    Rule::exists('fly_gourmet_meal_classify','id')
                        ->where('shop_id',$shop_info->id),
                    "integer",
                    "min:1",
                ],
                'product_name'=>[
                    Rule::unique('fly_product','product_name')
                        ->where('type','2')
                        ->where('shop_id',$shop_info->id)
                        ->whereNull('deleted_at'),
                    "required",
                    "string",
                    "min:1",
                    "max:255",
                ],
                'fileinfo_id'=> [
                    "integer",
                    Rule::exists('fileinfo','fileinfo_id'),
                ],
                'main_pic' =>"required|array|max:6" ,
                'main_pic.*' => [
                    "required",
                    "integer",
                    Rule::exists('fileinfo','fileinfo_id'),
                ],
                'sku'=>"required|array|min:1",
                'sku.*.sku_name'=>[
                    "required",
                    "string",
                    "min:1",
                    "max:255",
                ],
                'sku.*.single_price'=>"required|numeric|min:0.01",
            ],
            );


        try {
            $request_p['created_at'] = $request_p['push_at']  =  date('Y-m-d H:i:s');
            $request_p['create_by'] = $this->getAdminId();
            $product_id =   $res = $this->gourmetMeal->add(intval($shop_info->id),$request_p);
            if($product_id){
                Db::commit();
                return $this->jsonResponse->success(['product_id'=>$product_id],'添加成功');
            }else{
                Db::rollBack();
                return $this->jsonResponse->error(ErrorCode::SYSTEM_INVALID,'添加失败1');
            }
        } catch (\Throwable $exception) {
            Db::rollBack();
            return $this->jsonResponse->error(ErrorCode::SYSTEM_INVALID,'添加失败2'.$exception->getMessage());
        }
    }

    public function edit()
    {

        $shop_info = CurrentShopData::getData();
        $request_p =  $this->request->inputs(['id','product_name','category_id','fileinfo_id','main_pic','sku']);
        $this->validation($request_p,
            [
                'id'=>[
                    "required",
                    "integer",
                    "min:1",
                    Rule::exists('fly_product')->where('shop_id',$shop_info->id)->whereNull('deleted_at'),
                ],
                'category_id'=>[
                    Rule::exists('fly_gourmet_meal_classify','id')
                        ->where('shop_id',$shop_info->id),
                    "integer",
                    "min:1",
                ],
                'product_name'=>[
                    Rule::unique('fly_product','product_name')
                        ->where('type','2')
                        ->where('shop_id',$shop_info->id)
                        ->whereNull('deleted_at')->ignore($request_p['id']),
                    "required",
                    "string",
                    "min:1",
                    "max:255",
                ],
                'fileinfo_id'=> [
                    "integer",
                    Rule::exists('fileinfo','fileinfo_id'),
                ],
                'main_pic' =>"required|array|max:6" ,
                'main_pic.*' => [
                    "required",
                    "integer",
                    Rule::exists('fileinfo','fileinfo_id'),
                ],
                'sku'=>"required|array|min:1",
                'sku.*.sku_name'=>[
                    "required",
                    "string",
                    "min:1",
                    "max:255",
                ],
                'sku.*.single_price'=>"required|numeric|min:0.01",
            ],
            );
        $request_p['updated_at'] = $request_p['push_at'] = date('Y-m-d H:i:s');
        $request_p['update_by'] = $this->getAdminId();
        try {
            $request_p['updated_at'] = $request_p['push_at'] = date('Y-m-d H:i:s');
            $request_p['update_by'] = $this->getAdminId();
            $up_res = $this->gourmetMeal->update(intval($shop_info->id),$request_p);
            if($up_res){
                Db::commit();
                return $this->jsonResponse->success($up_res,'修改成功');
            }else{
                Db::rollBack();
                return $this->jsonResponse->error(ErrorCode::SYSTEM_INVALID,'修改失败');
            }
        } catch (\Throwable $exception) {
            Db::rollBack();
            return $this->jsonResponse->error(ErrorCode::SYSTEM_INVALID,'修改失败2'.$exception->getMessage());
        }
    }


    /***
 *                                         ,s555SB@@&
 *                                      :9H####@@@@@Xi
 *                                     1@@@@@@@@@@@@@@8
 *                                   ,8@@@@@@@@@B@@@@@@8
 *                                  :B@@@@X3hi8Bs;B@@@@@Ah,
 *             ,8i                  r@@@B:     1S ,M@@@@@@#8;
 *            1AB35.i:               X@@8 .   SGhr ,A@@@@@@@@S
 *            1@h31MX8                18Hhh3i .i3r ,A@@@@@@@@@5
 *            ;@&i,58r5                 rGSS:     :B@@@@@@@@@@A
 *             1#i  . 9i                 hX.  .: .5@@@@@@@@@@@1
 *              sG1,  ,G53s.              9#Xi;hS5 3B@@@@@@@B1
 *               .h8h.,A@@@MXSs,           #@H1:    3ssSSX@1
 *               s ,@@@@@@@@@@@@Xhi,       r#@@X1s9M8    .GA981
 *               ,. rS8H#@@@@@@@@@@#HG51;.  .h31i;9@r    .8@@@@BS;i;
 *                .19AXXXAB@@@@@@@@@@@@@@#MHXG893hrX#XGGXM@@@@@@@@@@MS
 *                s@@MM@@@hsX#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@&,
 *              :GB@#3G@@Brs ,1GM@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@B,
 *            .hM@@@#@@#MX 51  r;iSGAM@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@8
 *          :3B@@@@@@@@@@@&9@h :Gs   .;sSXH@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@:
 *      s&HA#@@@@@@@@@@@@@@M89A;.8S.       ,r3@@@@@@@@@@@@@@@@@@@@@@@@@@@r
 *   ,13B@@@@@@@@@@@@@@@@@@@5 5B3 ;.         ;@@@@@@@@@@@@@@@@@@@@@@@@@@@i
 *  5#@@#&@@@@@@@@@@@@@@@@@@9  .39:          ;@@@@@@@@@@@@@@@@@@@@@@@@@@@;
 *  9@@@X:MM@@@@@@@@@@@@@@@#;    ;31.         H@@@@@@@@@@@@@@@@@@@@@@@@@@:
 *   SH#@B9.rM@@@@@@@@@@@@@B      @@@          3@@@@@@@@@@@@@@@@@@@@@@@@@@5
 *     ,:.   9@@@@@@@@@@@#HB5   @@@@@           .M@@@@@@@@@@@@@@@@@@@@@@B
 *           ,ssirhSM@&1;i19911i,.             s@@@@@@@@@@@@@@@@@@@@@@@@@@S
 *              ,,,rHAri1h1rh&@#353Sh:          8@@@@@@@@@@@@@@@@@@@@@@@@@#:
 *            .A3hH@#5S553&@@#h   i:i9S          #@@@@@@@@@@@@@@@@@@@@@@@@@A.
 *
 *
 *    妈妈，再爱我一次！
 */
}
